import java.util.Stack;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2021-10-18
 * Time: 0:15
 */
class Solution {
    public int calPoints(String[] ops) {
        //计分制采用栈来解决问题，遇到数字就入栈
        Stack<Integer> stack = new Stack();
        //遍历字符数组
        for(String op : ops) {
            if (op.equals("+")) {
                int top = stack.pop();//先弹出并保存最后一个元素
                int newtop = top + stack.peek();//让弹出的元素和的当前的栈顶元素相加
                stack.push(top);
                stack.push(newtop);
            } else if (op.equals("C")) {
                stack.pop();
            } else if (op.equals("D")) {
                stack.push(2 * stack.peek());//当前栈顶元素乘以2倍放入
            } else {
                stack.push(Integer.valueOf(op));//字符数字转为整型数字
            }
        }

        int ans = 0;
        for(int score : stack) ans += score;
        return ans;
    }
}